<?php defined('SYSPATH') OR die('No direct access allowed.');
/*
 * view template file using PHPTAL
 * 
 * @package    Application
 * @author     H.ARAI
 */

require_once Kohana::find_file('vendor', 'phptal/PHPTAL');

class PtalView_Core {
	
	//template path config
	protected static $_config;
	
	//config key
	const CONFIG_KEY_DIR = 'dir';
    const CONFIG_KEY_SUFF = 'suffix';
    const CONFIG_KEY_TEMP = 'tempDir';
    
	/*
	 * Constructor
	 * 
	 */
	public function __construct(){
		
		//load config
		self::$_config = Kohana::config('viewLocation');
		
	}
	
	/*
	 * fetch template file
	 * 
	 */
	public function fetch($tmpl, $data){

		$dir = self::$_config[self::CONFIG_KEY_DIR];
		$suffix = self::$_config[self::CONFIG_KEY_SUFF];
		$tempDir = self::$_config[self::CONFIG_KEY_TEMP];
		
		$path = sprintf("%s%s%s", $dir, $tmpl, $suffix);
		
		$phptal = new PHPTAL($path);
		$phptal->setPhpCodeDestination($tempDir);
		
		foreach ($data as $item => $value){
			$phptal->$item = $value;
		}
		
		$view = "";
		
		try{
		  $view = $phptal->execute();
		}catch (Exception $e){
		  throw new Kohana_Exception($e);	
		}
		
		return $view;
	}
	
    /*
     * view template file
     * 
     */
    public function view($tmpl, $data){

    	echo $this->fetch($tmpl, $data);
    	
    }
	
	
}
